<?php
declare (strict_types=1);

namespace app\mall\logic\address;

use app\common\exception\LogicException;
use app\mall\model\address\Address as AddressMod;
use ArrayObject;
use think\facade\Db;
use think\Model;

/**
 * 后台商品逻辑层
 */
class Address extends AddressMod
{
    /**
     * 新增地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @param array $param
     */
    public function addAddr(array $param)
    {
        // 数据验证
        $this->getValidate()->scene('add')->failException()->check($param);
        // 参数
        $param['user_id'] = static::$mid;

        $exist = $this->where('user_id', $param['user_id'])->where('areacode', $param['areacode'])->where('address', $param['address'])->where('consignee', $param['consignee'])->where('phone', $param['phone'])->findOrEmpty();

        $this->startTrans();
        try {
            if ($exist->isEmpty()) {
                $re = $this::create($param);
                if ($re->isEmpty()) throw new LogicException('新增地址失败');
                $id = $re->id;
            } else {
                $exist->areatext = $param['areatext'];
                $exist->default  = $param['default'];
                $exist->save();
                $id = $exist->id;
            }
            if ($param['default']) $this->cancelOgDeault($id);
            $this->commit();
        } catch (LogicException $e) {
            $this->rollback();
            throw new LogicException('新增地址失败');
        }
    }

    /**
     * 地址列表
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     */
    public function addrList()
    {
        return $this->where('user_id', static::$mid)->order(['default' => 'DESC', 'update_time' => 'DESC'])->select();
    }

    /**
     * 修改地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @param array $param
     */
    public function updateAddr(array $param)
    {
        // 数据验证
        $this->getValidate()->scene('update')->failException()->check($param);
        //
        $this->startTrans();
        try {
            $re = Address::update($param);
            if ($re->isEmpty()) throw new LogicException('地址修改失败');
            if ($param['default']) $this->cancelOgDeault($re->id);
            $this->commit();
        } catch (LogicException $e) {
            $this->rollback();
            throw new LogicException('地址修改失败');
        }
    }

    /**
     * 取消原有默认地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @param $id
     */
    public function cancelOgDeault($id)
    {
        $defaultData = $this->where('user_id', static::$mid)->where('id', '<>', $id)->where('default', 1)->findOrEmpty();
        if (!$defaultData->isEmpty()) {
            $defaultData->default = 0;
            if (!$defaultData->save()) throw new LogicException('默认地址处理失败');
        }
    }

    /**
     * 设置默认地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     */
    public function setDefault($id, $val)
    {
        $data = $this->findOrEmpty($id);
        if ($data->isEmpty()) throw new LogicException('数据不存在');
        $data->default = $val;
        $this->startTrans();
        try {
            if (!$data->save()) throw new LogicException('设置默认地址失败');
            $val && $this->cancelOgDeault($id);
            $this->commit();
        } catch (LogicException $e) {
            $this->rollback();
            throw new LogicException('设置默认地址失败');
        }
    }

    /**
     * 删除地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @param $id
     */
    public function delAddr($id)
    {
        $data = $this->findOrEmpty($id);
        if ($data->isEmpty()) throw new LogicException('数据不存在');
        if (!$data->delete()) throw new LogicException('地址删除失败');
    }

    /**
     * 获取用户订单默认使用地址
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @return Address|array|ArrayObject|Model
     */
    public function getUserOrderAddr()
    {
        $data = $this->field('id,consignee,phone,areacode,areatext,address')->where('user_id', static::$mid)->order(['default' => 'DESC', 'update_time' => 'DESC'])->findOrEmpty();
        return $data->isEmpty() ? new ArrayObject() : $data;
    }
}
